package com.woyowodaows.base;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.PopupWindow;

import com.woyowodaows.R;
import com.woyowodaows.base.listener.OnItemClickListener;
import com.woyowodaows.utils.DisplayUtils;


/**
 * @author haiwei
 *         公共popup都可继承此类
 *         2017/5/16
 */

public abstract class BasePopupwindow {

    protected Context mContext;
    protected MyPopupWindow mPopupwindow;
    protected View layoutViews;
    private boolean isHidePopupwindow = false;
    protected OnItemClickListener onItemClickListener;
    protected View views;
    private boolean isViewClickClose = true;


    public void setViewClickClose(boolean viewClickClose) {
        isViewClickClose = viewClickClose;
    }

    public BasePopupwindow(Context mContext, View views) {
        this.mContext = mContext;
        this.views = views;
        mPopupwindow = new MyPopupWindow(mContext);
    }


    class MyPopupWindow extends PopupWindow {

        public MyPopupWindow(final Context mContext) {
            layoutViews = View
                    .inflate(mContext, getLayoutId(), null);
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            setBackgroundDrawable(new BitmapDrawable());
            setFocusable(true);
            setOutsideTouchable(true);
            setContentView(layoutViews);

            //实例化一个ColorDrawable颜色为半透明
            ColorDrawable dw = new ColorDrawable(0x00000000);
            //设置SelectPicPopupWindow弹出窗体的背景
            this.setBackgroundDrawable(dw);
            backgroundAlpha((Activity) mContext, 0.5F);//0.0-1.0
            this.update();
            layoutViews.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isViewClickClose) {
                        hidePopupWindow();
                    }
                }
            });
            setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
            setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            initView(layoutViews);

            setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss() {
                    DisplayUtils.closeKeybord(layoutViews,mContext);
                    backgroundAlpha((Activity) mContext, 1f);//0.0-1.0
                    onDismis();
                }
            });


        }



    }



    //设置添加屏幕的背景透明度
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }


    public void  onDismis(){

    }


    /**
     * 初始化控件
     *
     * @param layoutViews
     */
    public abstract void initView(View layoutViews);

    /**
     * 获取布局数据
     *
     * @return
     */
    public abstract int getLayoutId();

    /**
     * 开启动画view
     */
    public abstract View animationView();

    /**
     * ....
     * 开启动画
     *
     * @param anim
     */
    private void startAnimation(int anim) {
        if (!animationView().isClickable()) {
            animationView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }
        Animation animation = AnimationUtils.loadAnimation(mContext, anim);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (isHidePopupwindow) {
                    onAnimationEnds(animation);
                    layoutViews.setEnabled(true);
                    if (mPopupwindow != null) {
                        mPopupwindow.dismiss();
                    }
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        animationView().startAnimation(animation);
    }

    /**
     * 显示弹框
     */
    public void showPopupWindow() {
        backgroundAlpha((Activity) mContext, 0.5F);//0.0-1.0
        isHidePopupwindow = false;
        startAnimation(R.anim.anim_popup_show);
        if (mPopupwindow != null) {
            mPopupwindow.showAtLocation(views, Gravity.BOTTOM, 0, 0);
        }

    }

    /**
     * 显示中间弹框
     */
    public void showCenterPopupWindow() {
        backgroundAlpha((Activity) mContext, 0.5F);//0.0-1.0
        isHidePopupwindow = false;
        startAnimation(R.anim.anim_popup_show);
        if (mPopupwindow != null) {
            mPopupwindow.showAtLocation(views, Gravity.CENTER, 0, 0);
        }

    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    public void hidePopupWindow() {
        if (mPopupwindow != null && mPopupwindow.isShowing()) {
            layoutViews.setEnabled(false);
            isHidePopupwindow = true;
            startAnimation(R.anim.anim_popup_hide);
        }
    }

    /**
     * 动画结束
     *
     * @param arg0
     */
    public void onAnimationEnds(Animation arg0) {

    }


    public void valuation(Object data) {

    }

    /**
     * 是否显示
     *
     * @return
     */
    public boolean isShow() {
        return mPopupwindow.isShowing();
    }

    /**
     * 普通显示
     */
    public void show() {
        backgroundAlpha((Activity) mContext, 0.5F);//0.0-1.0
        mPopupwindow.showAtLocation(views, Gravity.CENTER, 0, 0);
    }

    /**
     * 普通关闭
     */
    public void dismiss() {
        mPopupwindow.dismiss();
    }


    /**
     * 设置popup是否获得焦点
     *
     * @param focusable
     */
    public void setFocusable(boolean focusable) {
        mPopupwindow.setFocusable(focusable);
    }





    /**
     * 展示在view的下面
     * @param view
     */
    public void showAsDropDown(View view,int xoff, int yoff){
        if (Build.VERSION.SDK_INT >= 24) {//适配7.0系统展示问题
            Rect visibleFrame = new Rect();
            view.getGlobalVisibleRect(visibleFrame);
            int height = view.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            mPopupwindow.setHeight(height);
            mPopupwindow.showAsDropDown(view, xoff, yoff);
        } else {
            mPopupwindow.showAsDropDown(view);
        }
    }

}
